<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login_model
 *
 * @author bancan
 */
class Login_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function login($data) {

        $sth = $this->db->prepare("SELECT user_id,nrp,role FROM users 
                                    where 
                                    username = :username
                                    and
                                    password = :password");

        $sth->execute(array(
            ':username' => $data['username'],
            ':password' => Hash::create('md5', $data['password'], HASH_PASSWORD_KEY)
       
        ));

        $data = $sth->fetch();

//        print_r('$data');

        $count = $sth->rowCount();
        if ($count > 0) {
            // login

            Session::init();
            Session::set('loggedIn', true);
            Session::set('role', $data['role']);



            Session::set('nrp', $data['nrp']);

            header('location: ../dashboard');
        } else {
            header('location: ../login');
        }
    }

    public function captcha() {
        Session::init();
        header('Content-Type: image/jpeg');

//fungsi untuk membuat gambar
        $image = imagecreate(60, 30);

//menentukan warna background
        imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255)); //hitam
//menentukan warna text
        $warnatext = imagecolorallocate($image, 0, 0, 0); //putih
//mengenerate angka
        $angka1 = rand(0, 9);
        $angka2 = rand(0, 9);
        $angka3 = rand(0, 9);
        $angka4 = rand(0, 9);
        $code = $angka1 . $angka2 . $angka3 . $angka4;

//menyimpan kode dalam session
        session::set('captcha', $code);

//menulis kode pada gambar
        imagestring($image, 5, 18, 12, $code, $warnatext);

//output gambar
        $captcha = imagejpeg($image, "", 90);

//membebaskan memori
        imagedestroy($image);

        return $captcha;
    }

}

?>
